<template>
  <div>
    <!-- // 分配角色 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSbmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getRoles } from '@/api/settings'
import { getUserDateilId } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: [] // 保存所有角色的信息
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 获取所有角色信息
    async loadRoles() {
      try {
        const res = await getRoles()
        console.log('getRoles', res)
        this.list = res.data.rows
        // 发送请求获取当前角色的id
        const info = await getUserDateilId(this.curId)
        console.log('获取当前角色的id', info)
        this.roleIds = info.data.roleIds || []
        // 保存数据
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    // 确定
    async hSbmit() {
      try {
        const res = await assignRoles({ id: this.curId, roleIds: this.roleIds })
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.loadRoles()
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    // 取消
    closeDialog() {

    }
  }

}
</script>
